.SUFFIXES: .lo .to .tlo

prefix=@prefix@
exec_prefix=@exec_prefix@
bindir=@bindir@
sbindir=@sbindir@
datarootdir=@datarootdir@
libdir=@libdir@
includedir=@includedir@
mandir=@mandir@
mansubdir=@mansubdir@
sysconfdir=@sysconfdir@
piddir=@piddir@
srcdir=@srcdir@
top_srcdir=@top_srcdir@
buildtoolsdir=$(top_srcdir)/buildtools
host=@host@

DESTDIR=
VPATH=@srcdir@ $(top_srcdir)/daemon
SPREAD_PROGRAM=@sbindir@/spread
SP_MAJOR_VERSION=@SP_MAJOR_VERSION@
SP_MINOR_VERSION=@SP_MINOR_VERSION@
SP_PATCH_VERSION=@SP_PATCH_VERSION@
VERSION=@SP_MAJOR_VERSION@.@SP_MINOR_VERSION@.@SP_PATCH_VERSION@

#Shared Library version -- Must be changed upon each release 
#Rules: major  -- inc for incompatible change
#     : minor  -- inc for backwards compatible change (add new api, but not break or remove old one)
#     : bugfix -- inc for bugfix that is fully compatible

LIBFULLVERSION=2.0.1
LIBCOMPATVERSION=2.0
LIBVERSION=2

PATHS= 

CC=@CC@
LD=@LD@

# don't use debugging (asserts and dprintfs) (optional)
NO_DEBUG = -DNDEBUG
#flush defines
DEFINES= $(NO_DEBUG)

CFLAGS=@CFLAGS@ $(DEFINES)
#stdutil includes
FLUSH_INC_DIR = -I../stdutil/src -I$(top_srcdir)/stdutil/src -I$(srcdir) 
CPPFLAGS=-I$(top_srcdir)/daemon -I../daemon -I$(top_srcdir)/include $(FLUSH_INC_DIR) @CPPFLAGS@ $(PATHS) @DEFS@
LDFLAGS=@LDFLAGS@ 
LIBS=@LIBS@ 
THLDFLAGS=@THLDFLAGS@
THLIBS=@THLIBS@
AR=@AR@
LEX=@LEX@
SHCC=@SHCC@
SHLD=@SHLD@
SHCFLAGS=@SHCFLAGS@ $(DEFINES)
SHCPPFLAGS=@SHCPPFLAGS@ -I$(top_srcdir)/daemon -I../daemon -I$(top_srcdir)/include $(FLUSH_INC_DIR) 
SHLDFLAGS=@SHLDFLAGS@
SHLIBS=@SHLIBS@
SHLDOPTION=@SHLDOPTION@
SHLDNAME=@SHLDNAME@
SHLDCONVERTSTATIC=@SHLDCONVERTSTATIC@
SHLDCONVERTSTATICEND=@SHLDCONVERTSTATICEND@
RANLIB=@RANLIB@
INSTALL=@INSTALL@
SOFTLINK=@LN_S@
PERL=@PERL@
ENT=@ENT@
EXEEXT=@EXEEXT@

SP_OBJ_DIR=../daemon

TARGETS=libspread-core.a libtspread-core.a libspread.a @LIBSPSO@ @LIBSPCORESO@ @LIBTSPCORESO@

LIBSP_OBJS= $(SP_OBJ_DIR)/alarm.o $(SP_OBJ_DIR)/events.o $(SP_OBJ_DIR)/memory.o sp.o

LIBSP_SHOBJS= alarm.lo events.lo memory.lo sp.lo

LIBTSP_OBJS= alarm.to events.to memory.to sp.to

LIBTSP_SHOBJS= alarm.tlo events.tlo memory.tlo sp.tlo

LIBTFL_OBJS= fl.to scatp.to alarm.to events.to memory.to sp.to

LIBTFL_SHOBJS= fl.tlo scatp.tlo alarm.tlo events.tlo memory.tlo sp.tlo

# A bit of a hack to grab the -fPIC threaded object files to merge into the shared spread library
# since the stdutil.so cannot be merged, and the stdutil.a is not compiled -fPIC
# Can be fixed correctly if stdutil generates incremental shared library versions
STDUTIL_DIR=../stdutil/src

SHARED_STDUTIL= $(STDUTIL_DIR)/stdarr.lto $(STDUTIL_DIR)/stdcarr.lto $(STDUTIL_DIR)/stddll.lto $(STDUTIL_DIR)/stderror.lto $(STDUTIL_DIR)/stdfd.lto $(STDUTIL_DIR)/stdhash.lto $(STDUTIL_DIR)/stdit.lto $(STDUTIL_DIR)/stdskl.lto $(STDUTIL_DIR)/stdthread.lto $(STDUTIL_DIR)/stdtime.lto $(STDUTIL_DIR)/stdutil.lto

all: $(TARGETS)

.c.o:
	$(CC) $(CFLAGS) $(CPPFLAGS) -c $<

.c.to:
	$(CC) $(CFLAGS) $(CPPFLAGS) -D_REENTRANT -c $< -o $*.to

.c.lo:
	$(SHCC) $(SHCFLAGS) $(SHCPPFLAGS) -c $< -o $*.lo

.c.tlo:
	$(SHCC) $(SHCFLAGS) $(SHCPPFLAGS) -D_REENTRANT -c $< -o $*.tlo

none:	
	@echo "Skipping shared library creation."

install-none:
	@echo "Skipping shared library installation."

# libspread-core is the core spread library without Flush or Stdutil
libspread-core.a: $(LIBSP_OBJS)
	$(AR) rv $@ $(LIBSP_OBJS)
	$(RANLIB) $@

libspread-core.dylib:	$(LIBSP_SHOBJS)
	$(SHLD) -o libspread-core.$(LIBFULLVERSION).dylib $(LIBSP_SHOBJS) $(SHLDFLAGS) $(SHLIBS) $(SHLDNAME)libspread-core.$(LIBVERSION).dylib  -current_version $(LIBFULLVERSION) -compatibility_version $(LIBCOMPATVERSION)
	$(SOFTLINK) -f libspread-core.$(LIBFULLVERSION).dylib $@

install-libspread-core.dylib:	libspread-core.dylib
	$(INSTALL) -m 0755 libspread-core.$(LIBFULLVERSION).dylib $(DESTDIR)$(libdir)/libspread-core.$(LIBFULLVERSION).dylib
	(cd $(DESTDIR)$(libdir); \
		$(SOFTLINK) -f libspread-core.$(LIBFULLVERSION).dylib libspread-core.dylib; \
		$(SOFTLINK) -f libspread-core.$(LIBFULLVERSION).dylib libspread-core.$(LIBVERSION).dylib; \
		install_name_tool -id $(DESTDIR)$(libdir)/libspread-core.$(LIBVERSION).dylib $(DESTDIR)$(libdir)/libspread-core.$(LIBFULLVERSION).dylib \
	)

libspread-core.so:	$(LIBSP_SHOBJS)
	$(SHLD) -o $@ $(LIBSP_SHOBJS) $(SHLDFLAGS) $(SHLIBS) $(SHLDOPTION)$(SHLDNAME)$@.$(LIBVERSION)

install-libspread-core.so:	libspread-core.so
	$(INSTALL) -m 0755 libspread-core.so $(DESTDIR)$(libdir)/libspread-core.so.$(LIBFULLVERSION)
	(cd $(DESTDIR)$(libdir); \
		$(SOFTLINK) -f libspread-core.so.$(LIBFULLVERSION) libspread-core.so; \
		$(SOFTLINK) -f libspread-core.so.$(LIBFULLVERSION) libspread-core.so.$(LIBVERSION) \
	)

libtspread-core.a: $(LIBTSP_OBJS)
	$(AR) rv $@ $(LIBTSP_OBJS)
	$(RANLIB) $@

libtspread-core.dylib:	$(LIBTSP_SHOBJS)
	$(SHLD) -o libtspread-core.$(LIBFULLVERSION).dylib $(LIBTSP_SHOBJS) $(SHLDFLAGS) $(SHLIBS) $(THLIBS) $(SHLDNAME)libtspread-core.$(LIBVERSION).dylib  -current_version $(LIBFULLVERSION) -compatibility_version $(LIBCOMPATVERSION)
	$(SOFTLINK) -f libtspread-core.$(LIBFULLVERSION).dylib $@

install-libtspread-core.dylib:	libtspread-core.dylib
	$(INSTALL) -m 0755 libtspread-core.$(LIBFULLVERSION).dylib $(DESTDIR)$(libdir)/libtspread-core.$(LIBFULLVERSION).dylib
	(cd $(DESTDIR)$(libdir); \
		$(SOFTLINK) -f libtspread-core.$(LIBFULLVERSION).dylib libtspread-core.dylib; \
		$(SOFTLINK) -f libtspread-core.$(LIBFULLVERSION).dylib libtspread-core.$(LIBVERSION).dylib; \
		install_name_tool -id $(DESTDIR)$(libdir)/libtspread-core.$(LIBVERSION).dylib $(DESTDIR)$(libdir)/libtspread-core.$(LIBFULLVERSION).dylib \
	)

libtspread-core.so:	$(LIBTSP_SHOBJS)
	$(SHLD) -o $@ $(LIBTSP_SHOBJS) $(SHLDFLAGS) $(SHLIBS) $(THLIBS) $(SHLDOPTION)$(SHLDNAME)$@.$(LIBVERSION)

install-libtspread-core.so:	libtspread-core.so
	$(INSTALL) -m 0755 libtspread-core.so $(DESTDIR)$(libdir)/libtspread-core.so.$(LIBFULLVERSION)
	(cd $(DESTDIR)$(libdir); \
		$(SOFTLINK) -f libtspread-core.so.$(LIBFULLVERSION) libtspread-core.so; \
		$(SOFTLINK) -f libtspread-core.so.$(LIBFULLVERSION) libtspread-core.so.$(LIBVERSION) \
	)

# Full Spread library with Fl_, SP_, and support modules linked in
libspread.a: $(LIBTFL_OBJS) ../stdutil/lib/libstdutil-threaded-release.a
	$(AR) rv $@ $(LIBTFL_OBJS)
	$(AR) x ../stdutil/lib/libstdutil-threaded-release.a
	$(AR) rv $@ std*.to
	$(RANLIB) $@

libspread.dylib:	$(LIBTFL_SHOBJS)
	$(SHLD) -o libspread.$(LIBFULLVERSION).dylib $(LIBTFL_SHOBJS) $(SHLDFLAGS) $(SHLDCONVERTSTATIC) ../stdutil/lib/libstdutil-threaded-release.a $(SHLDCONVERTSTATICEND) $(SHLIBS) $(THLIBS) $(SHLDNAME)libspread.$(LIBVERSION).dylib  -current_version $(LIBFULLVERSION) -compatibility_version $(LIBCOMPATVERSION)
	$(SOFTLINK) -f libspread.$(LIBFULLVERSION).dylib $@

install-libspread.dylib:	libspread.dylib
	$(INSTALL) -m 0755 libspread.$(LIBFULLVERSION).dylib $(DESTDIR)$(libdir)/libspread.$(LIBFULLVERSION).dylib
	(cd $(DESTDIR)$(libdir); \
		$(SOFTLINK) -f libspread.$(LIBFULLVERSION).dylib libspread.dylib; \
		$(SOFTLINK) -f libspread.$(LIBFULLVERSION).dylib libspread.$(LIBVERSION).dylib; \
		install_name_tool -id $(DESTDIR)$(libdir)/libspread.$(LIBVERSION).dylib $(DESTDIR)$(libdir)/libspread.$(LIBFULLVERSION).dylib \
	)

libspread.so:	$(LIBTFL_SHOBJS)
	$(SHLD) -o $@ $(LIBTFL_SHOBJS) $(SHLDFLAGS) $(SHARED_STDUTIL) $(SHLIBS) $(THLIBS) $(SHLDOPTION)$(SHLDNAME)$@.$(LIBVERSION)

install-libspread.so:	libspread.so
	$(INSTALL) -m 0755 libspread.so $(DESTDIR)$(libdir)/libspread.so.$(LIBFULLVERSION)
	(cd $(DESTDIR)$(libdir); \
		$(SOFTLINK) -f libspread.so.$(LIBFULLVERSION) libspread.so; \
		$(SOFTLINK) -f libspread.so.$(LIBFULLVERSION) libspread.so.$(LIBVERSION); \
	)

clean:
	rm -f *.lo *.tlo *.to *.o *.a *.dylib *.so $(TARGETS)
	rm -f core
	rm -rf autom4te.cache
	rm -rf ../lib/$(host)

distclean: clean
	rm -f Makefile config.status *~

binrelease: $(TARGETS)
	$(buildtoolsdir)/mkinstalldirs ../lib/$(host)
	$(INSTALL) -m 644 libspread.a ../lib/$(host)/libspread.a
	$(INSTALL) -m 644 libspread-core.a ../lib/$(host)/libspread-core.a
	$(INSTALL) -m 644 libtspread-core.a ../lib/$(host)/libtspread-core.a
ifeq (@LIBSPSO@, libspread.so)
	  $(INSTALL) -m 755 libspread.so ../lib/$(host)/libspread.so.$(LIBFULLVERSION)
	  $(INSTALL) -m 755 libspread-core.so ../lib/$(host)/libspread-core.so.$(LIBFULLVERSION)
	  $(INSTALL) -m 755 libtspread-core.so ../lib/$(host)/libtspread-core.so.$(LIBFULLVERSION)
endif
ifeq (@LIBSPSO@, libspread.dylib)
	  $(INSTALL) -m 755 libspread.$(LIBFULLVERSION).dylib ../lib/$(host)/libspread.$(LIBFULLVERSION).dylib
	  $(INSTALL) -m 755 libspread-core.$(LIBFULLVERSION).dylib ../lib/$(host)/libspread-core.$(LIBFULLVERSION).dylib
	  $(INSTALL) -m 755 libtspread-core.$(LIBFULLVERSION).dylib ../lib/$(host)/libtspread-core.$(LIBFULLVERSION).dylib
endif

install: $(TARGETS) install-files install-@LIBSPSO@ install-@LIBSPCORESO@ install-@LIBTSPCORESO@

install-files:
	$(buildtoolsdir)/mkinstalldirs $(DESTDIR)$(libdir)
	$(buildtoolsdir)/mkinstalldirs $(DESTDIR)$(includedir)
	$(INSTALL) -m 644 libspread.a $(DESTDIR)$(libdir)/libspread.a
	$(INSTALL) -m 644 libspread-core.a $(DESTDIR)$(libdir)/libspread-core.a
	$(INSTALL) -m 644 libtspread-core.a $(DESTDIR)$(libdir)/libtspread-core.a
	$(INSTALL) -m 644 $(top_srcdir)/include/sp.h $(DESTDIR)$(includedir)/sp.h
	$(INSTALL) -m 644 $(top_srcdir)/include/sp_func.h $(DESTDIR)$(includedir)/sp_func.h
	$(INSTALL) -m 644 $(top_srcdir)/include/sp_events.h $(DESTDIR)$(includedir)/sp_events.h
	$(INSTALL) -m 644 $(top_srcdir)/include/fl.h $(DESTDIR)$(includedir)/fl.h

uninstallall:	uninstall
	-rmdir $(DESTDIR)$(libdir)

uninstall: 
	-rm -f $(DESTDIR)$(libdir)/libspread.a
	-rm -f $(DESTDIR)$(libdir)/libspread-core.a
	-rm -f $(DESTDIR)$(libdir)/libtspread-core.a
	-rm -f $(DESTDIR)$(libdir)/@LIBSPSO@
	-rm -f $(DESTDIR)$(libdir)/@LIBSPCORESO@
	-rm -f $(DESTDIR)$(libdir)/@LIBTSPCORESO@
	-rm -f $(DESTDIR)$(includedir)/sp.h
	-rm -f $(DESTDIR)$(includedir)/sp_func.h
	-rm -f $(DESTDIR)$(includedir)/sp_events.h
	-rm -f $(DESTDIR)$(includedir)/fl.h
